<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/style/admin.css" media="all">
</head>

<body>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">设备更换</div>
                <div class="layui-card-body">
                    <div style="margin-bottom: 10px;">
                        <div class="layui-input-item" style="float:left;padding-right:10px;">
                            <form class="layui-form">
                                <div class="layui-inline">
                                    <input id="search-device-name" type="text" placeholder="请输入设备名称"
                                           class="layui-input layui-input-search">
                                </div>
                                <div class="layui-inline">
                                    <a id="btn-search" class="layui-btn">查找</a>
                                </div>
                            </form>
                        </div>
                        <div style="float:right;">
                            <a id="btn-add" class="layui-btn">创建</a>
                            <a id="btn-batch-delete" class="layui-btn layui-btn-danger">删除</a>
                            <a id="btn-refresh" class="layui-btn layui-btn-primary">刷新</a>
                        </div>
                        <div style="clear:both;"></div>
                    </div>
                    <table class="layui-hide" id="device-replace-table" lay-filter="device-replace-table"></table>
                    <script type="text/html" id="toolbar-operation">
                        <a class="layui-btn layui-btn-xs" lay-event="detail">查看</a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="device-replace-form">
    <div class="layui-card">
        <div class="layui-card-header">设备更换详情</div>
        <div class="layui-card-body">
            <form class="layui-form" action="#" lay-filter="device-replace-form">
                <input type="hidden" name="id">
                <input type="hidden" id="device-name" name="deviceName">
                <div class="layui-form-item">
                    <label class="layui-form-label">*建筑群</label>
                    <div class="layui-input-block">
                        <select id="tunnelId" name="tunnelId" lay-filter="tunnelId"
                                lay-verify="required" lay-search>
                            <option value="">选择隧道</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">*更换设备</label>
                    <div class="layui-input-block">
                        <select id="deviceId" name="deviceId" lay-filter="deviceId"
                                lay-verify="required" lay-search>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">更换前品牌</label>
                    <div class="layui-input-block">
                        <input type="text" id="brand1" name="brand1" class="layui-input" readonly>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">更换前型号</label>
                    <div class="layui-input-block">
                        <input type="text" id="model1" name="model1" class="layui-input" readonly>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">更换前规格</label>
                    <div class="layui-input-block">
                        <input type="text" id="spec1" name="spec1" class="layui-input" readonly>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">更换前出厂编号</label>
                    <div class="layui-input-block">
                        <input type="text" id="product-no1" name="productNo1" class="layui-input" readonly>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">更换前生产日期</label>
                    <div class="layui-input-block">
                        <input type="text" id="product-date1" name="productDate1" class="layui-input" readonly>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">更换人员</label>
                    <div class="layui-input-block">
                        <select id="replace-by-id" name="replaceById" lay-search>
                            <option value="">更换人员</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">备注</label>
                    <div class="layui-input-block">
                        <input type="text" name="note1" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="form-submit">确定</button>
                </div>
            </form>
        </div>
    </div>
</script>
<script src="/layui/layui.js"></script>
<script>
    layui.config({
        base: '/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table', 'form', 'laydate'], function () {
        var $ = layui.$;
        var table = layui.table;
        var form = layui.form;
        var admin = layui.admin;
        var laydate = layui.laydate;
        var popupIndex;

        function loadTunnels(callback) {
            $.get('tunnels', function (result) {
                if (callback) {
                    callback(result.data);
                }
            });
        }

        function loadUsers(callback) {
            $.get('users', function (result) {
                if (callback) {
                    callback(result.data);
                }
            });
        }

        function loadDevices(tunnelId, callback) {
            $.get('/devices', {tunnelId}, function (result) {
                if (callback) {
                    callback(result.data);
                }
            });
        }

        function loadDevice(deviceId, callback) {
            $.get('/devices/' + deviceId, function (result) {
                if (callback) {
                    callback(result.data);
                }
            });
        }

        table.render({
            elem: '#device-replace-table',
            url: 'device-replaces',
            title: '',
            skin: 'line',
            cols: [[
                {type: 'checkbox'},
                {
                    title: '序号', width: 60, templet: function (d) {
                        return d.LAY_TABLE_INDEX + 1;
                    }
                },
                {field: 'no', title: '工单号'},
                {field: 'deviceName', title: '设备名称'},
                {field: 'createTime', title: '创建时间'},
                {field: 'replaceTime', title: '更换时间'},
                {field: 'stateText', title: '状态'},
                {fixed: 'right', title: '操作', toolbar: '#toolbar-operation'}
            ]],
            page: true
        });

        function showDeviceReplaceForm() {
            popupIndex = admin.popupRight({
                area: '560px',
                success: function () {
                    var formHtml = $('#device-replace-form').html();
                    $('#' + this.id).html(formHtml);
                    loadTunnels(function (res) {
                        res.forEach(function (item) {
                            $('#tunnelId').append('<option value="' + item.id + '">' + item.name + '</option>');
                        });
                        form.render();
                        form.on('select(tunnelId)', function (data) {
                            loadDevices(data.value, function (res) {
                                $('#deviceId').empty();
                                $('#deviceId').append('<option value="">选择设备</option>');
                                res.forEach(function (item) {
                                    $('#deviceId').append('<option value="' + item.id + '">' + item.name + '</option>');
                                });
                                form.render();
                                form.on('select(deviceId)', function (data) {
                                    loadDevice(data.value, function (res) {
                                        $('#device-name').val(res.name);
                                        $('#brand1').val(res.brand);
                                        $('#model1').val(res.model);
                                        $('#spec1').val(res.spec);
                                        $('#product-no1').val(res.productNo);
                                        $('#product-date1').val(res.productDate);
                                    });
                                });
                            });
                        });
                    });
                    loadUsers(function (res) {
                        res.forEach(function (item) {
                            $('#replace-by-id').append('<option value="' + item.id + '">' + item.name + '</option>');
                        });
                        form.render();
                    });
                    form.render();
                }
            });
            return popupIndex;
        }

        function closePopupForm() {
            if (popupIndex) {
                layer.close(popupIndex);
            }
        }

        function search() {
            table.reload('device-replace-table', {
                where: {
                    deviceName: $('#search-device-name').val(),
                },
                page: {
                    curr: 1
                }
            });
        }

        $(document).on('keypress', function (e) {
            if (e.which == 13) {
                search();
            }
        });

        $("#btn-search").on('click', function () {
            search();
        });

        $("#btn-add").on('click', function () {
            popupIndex = showDeviceReplaceForm();
        });

        $("#btn-refresh").on('click', function () {
            table.reload('device-replace-table');
            layer.msg('表格已刷新');
        });

        $("#btn-batch-delete").on('click', function () {
            layer.confirm('确定要删除吗?', {icon: 3, title: '提示'}, function (index) {
                var ids = [];
                var data = table.checkStatus('device-replace-table').data;
                for (let i in data) {
                    ids.push(data[i].id);
                }
                $.ajax({
                    url: 'device-replaces/' + ids.toString(),
                    type: 'DELETE',
                    success: function (result) {
                        if (result.code == 200) {
                            layer.msg('删除成功');
                            table.reload('device-replace-table');
                        } else {
                            layer.msg('删除失败');
                        }
                    }
                });
            });
        });

        table.on('tool(device-replace-table)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('真的删除该记录吗？', function (index) {
                    $.ajax({
                        url: 'device-replaces/' + data.id,
                        type: 'DELETE',
                        success: function (result) {
                            if (result.code == 200) {
                                layer.msg('删除成功');
                                table.reload('device-replace-table');
                            } else {
                                layer.msg('删除失败');
                            }
                        }
                    });
                    layer.close(index);
                });
            } else if (obj.event === 'detail') {
                layer.open({
                    title: '设备更换详情',
                    type: 2,
                    content: 'device-replace-info.html?id=' + data.id,
                    area: ['960px', '620px'],
                    maxmin: true,
                    end: function () {
                        table.reload('device-replace-table');
                    }
                });
            }
        });

        form.on('submit(form-submit)', function (data) {
            $.ajax({
                url: '/device-replaces/create',
                type: 'POST',
                data: data.field,
                success: function (result) {
                    if (result.code == 200) {
                        layer.msg('操作成功');
                        table.reload('device-replace-table');
                    } else {
                        layer.msg('提交失败');
                        console.error('提交失败');
                    }
                }
            });
            closePopupForm();
            return false;
        });

    });
</script>
</body>

</html>